package com.example.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.example.myapplication.R;

import java.util.List;
import java.util.Random;

public class AIDLActivity extends AppCompatActivity {

    private static final String TAG = "AIDL";

    private IBookManager iBookManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl);
        MutableLiveData<String> liveData = new MutableLiveData<>();
        liveData.setValue("");

        liveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

            }
        });

        findViewById(R.id.bind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AIDLActivity.this, BookService.class);
                bindService(intent, connection, Context.BIND_AUTO_CREATE);
            }
        });
        findViewById(R.id.unbind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // java.lang.IllegalArgumentException: Service not registered:
                unbindService(connection);
            }
        });
        findViewById(R.id.add_book).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (iBookManager != null) {
                    try {
                        Log.d(TAG, "addBook: before");
                        iBookManager.addBook(new Book("AA",new Random().nextInt(10)));
                        Log.d(TAG, "addBook: after");
                    } catch (RemoteException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        findViewById(R.id.get_book).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (iBookManager != null) {
                    try {
                        List<Book> bookList = iBookManager.getBookList();
                        Log.d(TAG, "getBookList: " + bookList);
                    } catch (RemoteException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG, "onServiceConnected: " + iBinder);
            iBookManager = IBookManager.Stub.asInterface(iBinder);
            /*try {
                iBinder.linkToDeath(new IBinder.DeathRecipient() {
                    @Override
                    public void binderDied() {
                        Log.d(TAG, "binderDied: " );
                    }
                },0);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }*/
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG, "onServiceDisconnected: " );
            iBookManager = null;
        }
    };
}
